package com.xhs.main.controller.rsb;

import com.alibaba.fastjson.JSONObject;
import com.xhs.main.entity.rsb.RsbContract;
import com.xhs.main.entity.rsb.RsbLayui;
import com.xhs.main.service.rsb.impl.RsbContractServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author author
 * @create 2020-10-24-14:10
 */
@Controller
public class RsbContractController {
    @Autowired
    RsbContractServiceImpl rsbContractService;

    @ResponseBody
    @PostMapping("/insertContract")
    public int insertContract(RsbContract rsbContract,String rsbConEnd2) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date rsbConEnd = format.parse(rsbConEnd2);
        rsbContract.setRsbConEnd(rsbConEnd);
        rsbContractService.insertContract(rsbContract);
        return 1;
    }

    @ResponseBody
    @RequestMapping("/deleteContract")
    public void deleteContract(String rsbContractNum){
        rsbContractService.deleteContract(rsbContractNum);
    }

    @ResponseBody
    @RequestMapping("/showAllContract")
    public RsbLayui showAllContract() throws ParseException {
        List<RsbContract> rsbContracts = rsbContractService.selectChoose(null, null, null);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        for(int i=0;i<rsbContracts.size();i++){
            String format1 = format.format(rsbContracts.get(i).getRsbConEnd());
            Date parse = format.parse(format1);
            rsbContracts.get(i).setRsbConEnd(parse);
        }
        return new RsbLayui(0,"",1000,rsbContracts);
    }

    @ResponseBody
    @RequestMapping("/showSearchContract")
    public RsbLayui showSearchContract(@RequestParam("condition")String condition) throws ParseException {
        JSONObject object = JSONObject.parseObject(condition);
        String rsbConNum= (String) object.get("rsbConNum");
        String date1= (String) object.get("rsbConEnd1");
        String date2= (String) object.get("rsbConEnd2");

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date rsbConEnd1=null;
        Date rsbConEnd2=null;
        if(!date1.equals("")){
            rsbConEnd1= format.parse(date1);
        }
        if(!date2.equals("")){
            rsbConEnd2= format.parse(date2);
        }
        List<RsbContract> rsbContracts = rsbContractService.selectChoose(rsbConNum, rsbConEnd1, rsbConEnd2);
        return new RsbLayui(0,"",1000,rsbContracts);
    }





}
